package com.zh.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.Exception.DuplicateException;
import com.zh.common.Result;
import com.zh.entity.Employee;
import com.zh.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author 你爹
 * @since 2023-09-21
 */
@RestController
@RequestMapping("/employee")
@Api(tags = "员工接口")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    @ApiOperation("员工登录")
    public Result<Employee> login(HttpServletRequest httpServletRequest, @RequestBody Employee employee) {
        HttpSession session = httpServletRequest.getSession();
        //1.查找用户名
        LambdaUpdateWrapper<Employee> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(wrapper);
        //2.判断用户是否存在
        if (emp == null) {
            return Result.error("账户不存在!");
        }
        //3.对比密码
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if (!emp.getPassword().equals(password)) {
            return Result.error("密码错误!");
        }
        //4.是否被禁用
        if (emp.getStatus() == 0) {
            return Result.success();
        }
        //5.登陆成功 id存session
        session.setAttribute("employee", emp.getId());
        return Result.success(emp);
    }

    @PostMapping("/logout")
    public Result<String> logOut(HttpServletRequest httpServletRequest) {
        httpServletRequest.getSession().removeAttribute("employee");
        return Result.success("退出登录");
    }

    @PostMapping
    public Result save(HttpServletRequest request, @RequestBody Employee employee) throws  DuplicateException {
        log.info("新增员工:{}", employee.toString());
        //填充字段
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        LambdaUpdateWrapper<Employee> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(wrapper);
        if (emp != null) {
            throw new DuplicateException(emp.getUsername());
        }
        employeeService.save(employee);
        return Result.success();
    }

    @GetMapping("/page")
    public Result<Page<Employee>> page(Integer page, Integer pageSize, String name) {
        pageSize=5;
        log.info("page:{} pageSize:{} name:{}", page, pageSize, name);
        Page<Employee> p = new Page<>(page, pageSize);
        LambdaUpdateWrapper<Employee> wrapper = new LambdaUpdateWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        wrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(p, wrapper);
        log.info("total:{},records:{}", p.getTotal(), p.getRecords());
        return Result.success(p);
    }

    @PutMapping
    public Result update(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("employee:{}", employee);

//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        employeeService.updateById(employee);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }

}

